import { GSprite } from "../../../../../com/mui/controls/GSprite";
import { LoaderHelp } from "../../../../common/display/LoaderHelp";
import { ResFileConst } from "../../../resource/ResFileConst";
import { ObjCreate } from "../ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
import { ArrayUtil } from "../../../../../com/gengine/utils/ArrayUtil";
import { FontUtil } from "../../../../common/font/FontUtil";
import { Vector } from "../../../../../__AS3__/vec/Vector";
type int = number;
//class VerifySprite
    
    export  class VerifySprite extends GSprite
    {
        constructor()
        {
            super();
            return;
        }

        protected /* override */ createDisposedChildrenImpl(): void
        {
            super.createDisposedChildrenImpl();
            LoaderHelp.addResCallBack(ResFileConst.VerifyBg, this.onLoaded);
            return;
        }

        protected onLoaded(): void
        {
            if (!_disposed) 
            {
                this._isResCompl = true;
                this._bg = ObjCreate.createBitmap(ImagesConst.VerifyBg, 0, 0, this);
                if (this._code) 
                {
                    this.code = this._code;
                }
            }
            return;
        }

        public get code(): string
        {
            return this._code;
        }

        public set code(arg1: string)
        {
            this._code = arg1;
            if (this._isResCompl) 
            {
                this.clean();
                this.changeBg();
                this.addCode(arg1);
            }
            return;
        }

        protected changeBg(): void
        {
            var loc1=Math.floor(Math.random() * 10);
            var loc2=flash.display.BitmapDataChannel.RED | flash.display.BitmapDataChannel.BLUE | flash.display.BitmapDataChannel.GREEN;
            this._bg.bitmapData.perlinNoise(256, 64, 4, loc1, false, false, loc2);
            return;
        }

        protected addCode(arg1: string): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=NaN;
            var loc5=NaN;
            var loc6=null;
            var loc7=NaN;
            var loc10=null;
            var loc11=NaN;
            var loc12=null;
            var loc8=0;
            var loc9=0;
            while (loc9 < arg1.length) 
            {
                loc7 = Math.random() * 6;
                loc8 = loc8 + loc7;
                loc1 = arg1.charAt(loc9);
                loc4 = loc8 - 5;
                loc8 = loc8 + 50;
                loc5 = -5 + Math.random() * 6;
                loc6 = new Object /* flash.text.TextFormat */(VerifySprite.randomFontName(), 35 + int(Math.random() * 4), VerifySprite.getRandomColor(), true);
                loc2 = ObjCreate.createTextField("", 0, 0, 60, 60, null, loc6);
                loc2.filters = [];
                loc2.text = loc1;
                loc3 = new Object /* flash.display.Bitmap */();
                (loc10 = new Object /* flash.display.BitmapData */(80, 80, true, 16777215)).draw(loc2, new Object /* flash.geom.Matrix */(1, 0, 0, 1, 10, 10));
                loc3.smoothing = true;
                loc10.merge(this._bg.bitmapData, new Object /* flash.geom.Rectangle */(Math.random() * (this._bg.width - 80), Math.random() * (this._bg.height - 80), 80, 80), new Object /* flash.geom.Point */(), 32, 32, 32, 0);
                loc3.bitmapData = loc10;
                this.addChild(loc3);
                loc7 = Math.random();
                loc11 = 0;
                loc11 = -20 + loc7 * 40;
                loc12 = new Object /* flash.geom.Matrix */();
                loc12.tx = loc12.tx - 22;
                loc12.ty = loc12.ty - 20;
                loc12.rotate(loc11 * Math.PI / 180);
                loc12.tx = loc12.tx + (22 + loc4);
                loc12.ty = loc12.ty + (20 + loc5);
                loc3.transform.matrix = loc12;
                ++loc9;
            }
            return;
        }

        protected addLine(): void
        {
            var loc1=[ImagesConst.VerifyLine1, ImagesConst.VerifyLine2, ImagesConst.VerifyLine3];
            var loc2=loc1[Math.floor(Math.random() * loc1.length)];
            var loc3=ObjCreate.createBitmap(loc2, 0, 0, this);
            var loc4=Math.random() * (this._bg.width - loc3.width);
            var loc5=Math.random() * (this._bg.height - loc3.height);
            loc3.x = loc4;
            loc3.y = loc5;
            return;
        }

        protected addPoint(): void
        {
            var loc3=null;
            var loc4=null;
            var loc5=NaN;
            var loc6=NaN;
            var loc1=[ImagesConst.VerifyPoint1, ImagesConst.VerifyPoint2, ImagesConst.VerifyPoint3, ImagesConst.VerifyPoint4, ImagesConst.VerifyPoint5, ImagesConst.VerifyPoint6];
            var loc2=0;
            while (loc2 < 10) 
            {
                loc3 = loc1[Math.floor(Math.random() * loc1.length)];
                loc4 = ObjCreate.createBitmap(loc3, 0, 0, this);
                loc5 = Math.random() * (this._bg.width - loc4.width);
                loc6 = Math.random() * (this._bg.height - loc4.height);
                loc4.x = loc5;
                loc4.y = loc6;
                ++loc2;
            }
            return;
        }

        public clean(): void
        {
            while (this.numChildren > 1) 
            {
                this.removeChildAt(1);
            }
            return;
        }

        protected /* override */ disposeImpl(arg1: Boolean=true): void
        {
            while (this.numChildren > 0) 
            {
                this.removeChildAt(0);
            }
            this._isResCompl = false;
            this._code = "";
            this._bg = null;
            super.disposeImpl(arg1);
            return;
        }

        public static getRandomColor(): int
        {
            var loc1=VerifySprite.colorVec[Math.floor(Math.random() * VerifySprite.colorVec.length)];
            var loc2=(loc1 & 16711680) >> 16;
            var loc3=(loc1 & 65280) >> 8;
            var loc4=loc1 & 255;
            var loc5=VerifySprite.randomColor(loc2);
            var loc6=VerifySprite.randomColor(loc3);
            var loc7=VerifySprite.randomColor(loc4);
            var loc8;
            return loc8 = (loc5 << 16) + (loc6 << 8) + loc7;
        }

        public static randomColor(arg1: int, arg2: int=16): int
        {
            arg1 = arg1 + Math.random() * arg2;
            arg1 = arg1 > 255 ? 255 : arg1;
            return arg1;
        }

        public static randomFontName(): string
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            var loc4=0;
            var loc5=0;
            var loc6=0;
            if (!VerifySprite.fontNameEffectVec) 
            {
                VerifySprite.fontNameEffectVec = new Vector.<String>();
                loc1 = flash.text.Font.enumerateFonts(true);
                loc2 = flash.text.Font.enumerateFonts(false);
                ArrayUtil.removeArray(loc1, loc2);
                loc3 = VerifySprite.fontNameVec.length;
                loc4 = loc1.length;
                loc5 = 0;
                while (loc5 < loc3) 
                {
                    loc6 = 0;
                    while (loc6 < loc4) 
                    {
                        if ((loc1[loc6] as Object /* flash.text.Font */).fontName == VerifySprite.fontNameVec[loc5]) 
                        {
                            VerifySprite.fontNameEffectVec.push(VerifySprite.fontNameVec[loc5]);
                        }
                        ++loc6;
                    }
                    ++loc5;
                }
            }
            if (VerifySprite.fontNameEffectVec.length) 
            {
                return VerifySprite.fontNameEffectVec[Math.floor(Math.random() * VerifySprite.fontNameEffectVec.length)];
            }
            return FontUtil.defaultName;
        }

        /* internal */ /* var */_bg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_code: string;

        /* internal */ /* var */_isResCompl: Boolean=false;

        /* internal */ static /* var */colorVec: Vector.<int>;

        /* internal */ static /* var */fontNameVec: Vector.<String>;

        /* internal */ static /* var */fontNameEffectVec: Vector.<String>;
    }

            VerifySprite.colorVec = Vector.<int>([6350622, 41727, 15859633, 15788607, 16711935, 16742658]);
            VerifySprite.fontNameVec = Vector.<String>(["Monotype Corsiva", "Comic Sans MS", "Arial Black", "Lucida Console", "仿宋_GB2312", "Bookman Old Style", "MingLiU", "Courier New"]);
        